package com.zhuoyue.biz.core.command.impl.intelligence;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.constant.SceneCmdDeal;
import com.zhuoyue.biz.core.command.model.SceneCmd;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author wjming
 * @ClassName: Scene
 * @Description: 场景配置
 * @Date 2021-07-14 10:53
 */
public class Scene {
    /**
     * 开始场景配置指令
     */
    private static final String CMD_MODEL_SET = "5000${gatewayMac}360100000000000000000000000000000000000000${first}01${sceneNumber}00${cmdCount}${sceneNumber}${time}FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000";
    /**
     * 场景配置指令加载
     */
    private static final String CMD_MODEL_LOAD = "5000${gatewayMac}${dataLength}02${sceneNumber}${cmdCount}${orderNumber}${loadCmd}${timeExpand}";

    /**
     * 场景删除
     */
    public static final String CMD_MODE_CLEAR = "5000${gatewayMac}0207${sceneNumber}";
    /**
     * 场景执行
     */
    private static final String CMD_MODE_DEAL = "5000${gatewayMac}0209${sceneNumber}";


    private static final String ALL_FF = "FF";

    public static String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");

        String cmd = "";
        //场景配置
        if (SceneCmdDeal.SceneDealType.SCENE_CONFIGURE.equals(controlType)) {
            cmd = getSceneConfigureCmd(map);
            //场景载入
        } else if (SceneCmdDeal.SceneDealType.SCENE_LOAD.equals(controlType)) {
            cmd = getSceneLoadCmd(map);

        } else if (SceneCmdDeal.SceneDealType.SCENE_TRIGGER.equals(controlType)) {
            cmd = getSceneTriggerCmd(map);
            //场景删除
        } else if (SceneCmdDeal.SceneDealType.SCENE_CLEAR.equals(controlType)) {
            cmd = getSceneClearCmd(map);
        }

        return cmd;
    }

    /**
     * 删除场景
     *
     * @param map
     * @return
     */
    private static String getSceneClearCmd(Map<String, String> map) {
        String gatewayMac = map.get("gatewayMac");
        String sceneNumber = map.get("sceneNumber");
        sceneNumber = CmdUtils.intToHexTwo(Integer.parseInt(sceneNumber));
        return new SceneCmd(gatewayMac, sceneNumber).getCmd(CMD_MODE_CLEAR);
    }


    /**
     * 场景配置
     *
     * @param map
     * @return
     */
    private static String getSceneConfigureCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        String first = map.get("first");
        Map<String, String> actionsMap;
        try {
            String actions = map.get("actions");
            actionsMap = JSON.parseObject(actions, Map.class);
        } catch (ClassCastException e) {
            Object action = map.get("actions");
            actionsMap = (Map<String, String>) action;
        }
        String sceneNumber = actionsMap.get("sceneNumber");
        String cmdCount = actionsMap.get("cmdCount");
        String fixedHour = actionsMap.get("fixedHour");
        String fixedMinute = actionsMap.get("fixedMinute");
        sceneNumber = CmdUtils.intToHexTwo(Integer.parseInt(sceneNumber));
        cmdCount = CmdUtils.intToHexTwo(Integer.parseInt(cmdCount));
        String time = "";
        if (ALL_FF.equals(fixedHour) && ALL_FF.equals(fixedMinute)) {
            time = "FFFF";
        } else {
            time = CmdUtils.intToHexTwo(Integer.parseInt(fixedHour)) + CmdUtils.intToHexTwo(Integer.parseInt(fixedMinute));
        }
        return new SceneCmd(gatewayMac, sceneNumber, cmdCount, StringUtils.isEmpty(first) ? "00" : first, time).getCmd(CMD_MODEL_SET);
    }

    /**
     * 场景载入
     *
     * @param map
     * @return
     */
    private static String getSceneLoadCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String sceneNumber = actionsMap.get("sceneNumber");
        String cmdCount = actionsMap.get("cmdCount");
        String orderNumber = actionsMap.get("orderNumber");
        String loadCmd = actionsMap.get("loadCmd");
        //如果没有会有一定的延时执行时间
        String timeExpand = actionsMap.get("timeExpand");

        sceneNumber = CmdUtils.intToHexTwo(Integer.parseInt(sceneNumber));

        cmdCount = CmdUtils.intToHexTwo(Integer.parseInt(cmdCount));

        orderNumber = CmdUtils.intToHexTwo(Integer.parseInt(orderNumber));

        timeExpand = CmdUtils.intToHexTwo(Integer.parseInt(timeExpand));

        String configCmd = "02" + sceneNumber + cmdCount + orderNumber + loadCmd;
        String dataLength = CmdUtils.getDateLength(configCmd + timeExpand);
        return new SceneCmd(gatewayMac, sceneNumber, cmdCount, dataLength, orderNumber, loadCmd, timeExpand).getCmd(CMD_MODEL_LOAD);
    }

    /**
     * 执行场景
     *
     * @param map
     * @return
     */
    private static String getSceneTriggerCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String sceneNumber = actionsMap.get("sceneNumber");

        sceneNumber = CmdUtils.intToHexTwo(Integer.parseInt(sceneNumber));

        return new SceneCmd(gatewayMac, sceneNumber).getCmd(CMD_MODE_DEAL);
    }


}
